import pygame

from packaging import version

__Version__ = version.Version('0.1.1-a1')
WIDTH = HEIGHT = 50
main_window_size = (1150, 750)
MOUSEDOWN = False
ClickedColor = (0, 0, 0)
background_color = (185, 98, 255)


class GlobalSetting:
    def __init__(self):
        self.run_flag = 1


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()


class Line(pygame.sprite.Sprite):
    def __init__(self, center, vehicle, color=(255, 255, 255)):
        super().__init__()
        if vehicle:
            size = (5, WIDTH)
            self.st = (2, 0)
            self.ed = (2, WIDTH - 1)
            # center[0] += 3
            center[1] += WIDTH / 2
        else:
            size = (HEIGHT, 5)
            self.st = (0, 2)
            self.ed = (WIDTH - 1, 2)
            center[0] += HEIGHT / 2
            # center[1] += 3
        self.color = color
        self.image = pygame.Surface(size)
        self.image.fill(background_color)

        self.rect = self.image.get_rect()
        self.rect.center = center

    def update(self):
        if MOUSEDOWN:
            if self.rect.collidepoint(pygame.mouse.get_pos()):
                self.color = ClickedColor
        pygame.draw.line(self.image, self.color, self.st, self.ed, 1)


def draw_table(screen, row, col):
    group = pygame.sprite.Group()
    sx = main_window_size[0] / 2 - row / 2 * WIDTH
    sy = main_window_size[1] / 2 - col / 2 * HEIGHT
    for j in range(col):
        group.add(Line([sx, sy + j * HEIGHT], 1, ClickedColor))
        group.add(Line([sx + row * WIDTH, sy + j * HEIGHT], 1, ClickedColor))
        for i in range(1, row):
            group.add(Line([sx + i * WIDTH, sy + j * HEIGHT], 1))
    for i in range(row):
        group.add(Line([sx + i * WIDTH, sy], 0, ClickedColor))
        group.add(Line([sx + i * WIDTH, sy + col * HEIGHT], 0, ClickedColor))
        for j in range(1, col):
            group.add(Line([sx + i * WIDTH, sy + j * HEIGHT], 0))
    return group


def main_loop():
    global MOUSEDOWN
    pygame.init()
    global_setting = GlobalSetting()

    screen = pygame.display.set_mode(main_window_size)
    clock = pygame.time.Clock()

    pygame.display.set_caption("Monster!")

    line_group = draw_table(screen, 10, 10)

    while global_setting.run_flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                global_setting.run_flag = 0
            if event.type == pygame.MOUSEBUTTONDOWN:
                MOUSEDOWN = True
            if event.type == pygame.MOUSEBUTTONUP:
                MOUSEDOWN = False

        screen.fill(background_color)

        line_group.update()
        line_group.draw(screen)

        pygame.display.update()
        clock.tick(60)


if __name__ == '__main__':
    print("Monster Game" + str(__Version__) + "@CloudDreamLaker")
    main_loop()
